﻿angular.module('paperless').controller('transactionViewController', function ($scope, Restangular, $stateParams, $state) {
    'use strict';

    var transactionId = $stateParams.id,
        transactionResource = Restangular.one('transaction', $stateParams.id),
        noteResource = transactionResource.all('note'),
        contactResource = transactionResource.all('contact');

    transactionResource.get().then(function (transaction) {
        $scope.transaction = transaction;
        $scope.agentSelectionList = transaction.assignAgents.map(function (agent) {
            return {
                value: agent.id,
                label: agent.name,
                email: agent.email
            };
        });
    });

    noteResource.getList().then(function (notes) {
        $scope.notes = notes;
    });

    contactResource.getList().then(function (contacts) {
        $scope.contacts = contacts;
    })

    $scope.deleteTransaction = function (item) {
        return item.remove().then(function () {
            $state.transitionTo('transaction');
        });
    };

    $scope.deleteContact = function (index) {
        var id = $scope.contacts[index].id;

        return contactResource
          .customDELETE(id)
          .then(function () {
              $scope.contacts.splice(index, 1);
          });
    };

    $scope.updateContact = function (item) {
        return contactResource
            .customPUT(item)
            .then(function () {
                item.$edit = undefined;
            });
    };

    $scope.editContact = function (item) {
        item.$edit = true;
    };

    $scope.cancelContact = function (item) {
        item.$edit = undefined;
    };

    $scope.showAddContact = function () {
        $scope.newItem = {$add: true};
    };

    $scope.cancelAddContact = function (item) {
        $scope.newItem = {};
    };

    $scope.addContact = function (item) {
        var postedItem = angular.extend({}, item);
        postedItem.transactionId = transactionId;

        return contactResource
            .post(postedItem)
            .then(function (id) {
                postedItem.id = id;
                $scope.contacts.push(postedItem);
                $scope.showAddContact();
            });
    };

    $scope.deleteNote = function (index) {
        var id = $scope.notes[index].id;

        return noteResource
          .customDELETE(id)
          .then(function () {
              $scope.notes.splice(index, 1);
          });
    }

    $scope.addNote = function (item) {
        var postedItem = angular.extend({}, item);

        return noteResource
            .post(postedItem)
            .then(function (id) {
                postedItem.agents = [];
                postedItem.id = id;
                postedItem.createdDate = new Date();
                postedItem.createdBy = 'me';

                angular.forEach($scope.agentSelectionList, function (agent) {
                    if (postedItem.agentIds.indexOf(agent.value) > -1) {
                        postedItem.agents.push({
                            name: agent.label,
                            email: agent.email
                        });
                    }
                });

                $scope.notes.push(postedItem);
                $scope.showAddNote();
            });
    };

    $scope.showAddNote = function () {
        $scope.newNote = {
            $show: true,
            agentIds: []
        };
    };

    $scope.hideAddNote = function (item) {
        $scope.newNote = {};
    };
});